package com.chenjiagou.ordertickets.controller;

import com.chenjiagou.ordertickets.entity.Airport;
import com.chenjiagou.ordertickets.service.AirportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/airport")
public class AirportController {
    @Resource
    private AirportService airportService;
    /**
     * GET /api/airports?keyword=bea
     */
    @GetMapping("/query")
    @ResponseBody
    public List<Map<String, Object>> queryAirports(@RequestParam String keyword) {
        List<Airport> airports = airportService.suggest(keyword);
        return airports.stream().map(airport -> {
            Map<String, Object> map = new HashMap<>();
            map.put("code", airport.getCode());
            map.put("cityName", airport.getCity());
            map.put("airportName", airport.getName());
            map.put("countryName", airport.getCountry());
            return map;
        }).collect(Collectors.toList());
    }
}
